package netty.pipeline.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestPipelineServer {
    public static void main(String[] args) {
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(new NioEventLoopGroup());
        serverBootstrap.channel(NioServerSocketChannel.class);
        serverBootstrap.childHandler(new ChannelInitializer<NioSocketChannel>() {
            @Override
            protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                // 通过channel获取pipeline
                ChannelPipeline pipeline = nioSocketChannel.pipeline();
                pipeline.addLast("h1",new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        log.debug("入栈-----1");
                        super.channelRead(ctx, msg);
                    }
                });

                pipeline.addLast("h2",new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        log.debug("入栈-----2");
                        super.channelRead(ctx, msg);
                    }
                });

                pipeline.addLast("h3",new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        log.debug("入栈-----3");
                        super.channelRead(ctx, msg);
                        nioSocketChannel.writeAndFlush(ctx.alloc().buffer().writeBytes("server....".getBytes()));
                    }
                });

                pipeline.addLast("h4",new ChannelOutboundHandlerAdapter() {

                    @Override
                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                        log.debug("出栈-----4");
                        super.write(ctx, msg, promise);
                    }
                });

                pipeline.addLast("h5",new ChannelOutboundHandlerAdapter() {

                    @Override
                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                        log.debug("出栈-----5");
                        super.write(ctx, msg, promise);
                    }
                });

                pipeline.addLast("h6",new ChannelOutboundHandlerAdapter() {

                    @Override
                    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                        log.debug("出栈-----6");
                        super.write(ctx, msg, promise);
                    }
                });
            }
        }).bind(8080);
    }
}
